package com.main.by.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.main.by.entity.bo.UserLoginBO;
import com.main.by.entity.vo.AdminLoginVO;
import com.main.by.entity.vo.NewsInfoVO;
import com.main.by.entity.vo.TaskVO;
import com.main.by.exception.ServiceException;
import com.main.by.service.AdminService;
import com.main.by.service.NewsInfoService;
import com.main.by.service.TaskService;
import com.main.by.service.UserService;
import com.main.by.util.MD5Util;
import com.main.by.util.StringUtil;

@Controller
@RequestMapping("/adminjsp/")
public class AdminDetailController {
	
	private Logger logger = Logger.getLogger(this.getClass());
	
	@Autowired
	private NewsInfoService newSInfoService;
	
	@Autowired
	private AdminService adminService;
	
	@Autowired
	private UserService UseRService;
	
	@Autowired
	private NewsInfoService newsInfoService;
	
	@Autowired
	private TaskService taskService;
	
	
	@RequestMapping(value = "index", method = RequestMethod.GET)
	public String login(HttpServletRequest request){
		logger.info("系统管理员登录");
		return "adminjsp/index";
	}
	
	@RequestMapping(value = "addNews", method = RequestMethod.GET)
	public String addNews(HttpServletRequest request){
		logger.info("系统管理员登录");
		return "adminjsp/addNews";
	}
	
	@RequestMapping(value = "newsList", method = RequestMethod.GET)
	public String newsList(HttpServletRequest request){
		logger.info("查看所有新闻信息-------");
		try {
			List<NewsInfoVO> newInfo = newSInfoService.findAllNews(request);
			request.getSession().setAttribute("newsInfList", newInfo);
		}catch (ServiceException serviceE){
			return "adminjsp/index";
		}catch (Exception e) {
			logger.error("select news failed! ", e);
			return "adminjsp/index";
		}

		return "adminjsp/newsList";
	}
	
	@RequestMapping(value = "modifyNews", method = RequestMethod.GET)
	public String modifyNews(HttpServletRequest request) throws IOException{
		String errorMessage = "修改失败";
		String newsId =  request.getParameter("newsId");
		int id = 0;
		if (StringUtil.isNotBlank(newsId)) {
			id = Integer.parseInt(newsId);
		}
		logger.info("根据ID查找新闻信息-------");
		NewsInfoVO newsInfo = new NewsInfoVO();
		try {
			newsInfo = newsInfoService.findNewsById(id);
			request.getSession().setAttribute("updateNewsInfo", newsInfo);
		}catch (ServiceException serviceE){
			errorMessage = serviceE.getMessage();
			return "adminjsp/newsList";
		}catch (Exception e) {
			logger.error("select admin failed! ", e);
			return "adminjsp/newsList";
		}
		return "adminjsp/modifyNews";
	}
	
	@RequestMapping(value = "deleteNews", method = RequestMethod.GET)
	public void deleteNews(HttpServletRequest request,HttpServletResponse response) throws IOException{
		request.setCharacterEncoding("utf8");
		response.setContentType("text/html;charset=utf8");
		String newsId =  request.getParameter("newsId");
		PrintWriter out = response.getWriter();
		int id = 0;
		if (StringUtil.isNotBlank(newsId)) {
			id = Integer.parseInt(newsId);
		}
		logger.info("根据ID查找新闻信息-------");
		try {
			newsInfoService.deleteNewsById(id);
			response.sendRedirect("newsList");
		}catch (ServiceException serviceE){
			out
			.print("<script type='text/javascript'>alert('删除失败');history.go(-1);</script>");
		}catch (Exception e) {
			logger.error("select admin failed! ", e);
			out
			.print("<script type='text/javascript'>alert('删除失败');history.go(-1);</script>");
		}
		
	}
	
	@RequestMapping(value = "adminManager", method = RequestMethod.GET)
	public String adminManager(HttpServletRequest request){
		
		logger.info("查看所有管理员信息-------");
		String newsId =  request.getParameter("newsId");
		try {
			List<AdminLoginVO> adminInfo = adminService.findAllAdmin(request);
			request.getSession().setAttribute("adminList", adminInfo);
		}catch (ServiceException serviceE){
			return "adminjsp/index";
		}catch (Exception e) {
			logger.error("select admin failed! ", e);
			return "adminjsp/index";
		}
		return "adminjsp/adminManager";
	}
	
	@RequestMapping(value = "/updateAdmin", method = RequestMethod.GET)
	public String updateAdmin(HttpServletRequest request) throws IOException{
		String errorMessage = "修改失败";
		String adminId =  request.getParameter("adminId");
		int id = 0;
		if (StringUtil.isNotBlank(adminId)) {
			id = Integer.parseInt(adminId);
		}
		logger.info("根据ID查找管理员信息-------");
		AdminLoginVO adminInfo = new AdminLoginVO();
		try {
			adminInfo = adminService.findAdminById(id);
			request.getSession().setAttribute("updateAdminInfo", adminInfo);
		}catch (ServiceException serviceE){
			errorMessage = serviceE.getMessage();
			return "adminjsp/adminManager";
		}catch (Exception e) {
			logger.error("select admin failed! ", e);
			return "adminjsp/adminManager";
		}
		return "adminjsp/updateAdmin";
	}
	
	@RequestMapping(value = "updateAdminMethod", method = RequestMethod.POST)
	@ResponseBody 
	public void updateAdminReset(HttpServletRequest request,HttpServletResponse response) throws IOException{
		request.setCharacterEncoding("utf8");
		response.setContentType("text/html;charset=utf8");
		String adminId =  request.getParameter("adminId");
		int id = 0;
		if (StringUtil.isNotBlank(adminId)) {
			id = Integer.parseInt(adminId);
		}
		String adminName =  request.getParameter("adminName");
		String oldadminPass =  request.getParameter("oldadminPass");
		String adminPass =  request.getParameter("adminPass");
		String adminPhone =  request.getParameter("adminPhone");
		PrintWriter out = response.getWriter();
		AdminLoginVO adminInfo = new AdminLoginVO();
		if (StringUtil.isNotBlank(adminPass)) {
			adminInfo.setAdminPass(MD5Util.GetMD5Code(adminPass));
		} else {
			adminInfo.setAdminPass(oldadminPass);
		}
			adminInfo.setAdminId(id);
			adminInfo.setAdminName(adminName);
			adminInfo.setAdminPhone(adminPhone);
			
		logger.info("根据ID修改管理员信息-------");
		try {
			adminService.updateAdminMethod(adminInfo);
			response.sendRedirect("adminManager");
		}catch (ServiceException serviceE){
			out
			.print("<script type='text/javascript'>alert('修改失败');history.go(-1);</script>");
		}catch (Exception e) {
			logger.error("select admin failed! ", e);
			out
			.print("<script type='text/javascript'>alert('修改失败');history.go(-1);</script>");
		}
		
	}
	
	@RequestMapping(value = "deleteAdmin", method = RequestMethod.GET)
	public void deleteAdmin(HttpServletRequest request,HttpServletResponse response) throws IOException{
		request.setCharacterEncoding("utf8");
		response.setContentType("text/html;charset=utf8");
		String adminId =  request.getParameter("adminId");
		PrintWriter out = response.getWriter();
		int id = 0;
		if (StringUtil.isNotBlank(adminId)) {
			id = Integer.parseInt(adminId);
		}
		logger.info("根据ID查找管理员信息-------");
		try {
			adminService.deleteAdminById(id);
			response.sendRedirect("adminManager");
		}catch (ServiceException serviceE){
			out
			.print("<script type='text/javascript'>alert('删除失败');history.go(-1);</script>");
		}catch (Exception e) {
			logger.error("select admin failed! ", e);
			out
			.print("<script type='text/javascript'>alert('删除失败');history.go(-1);</script>");
		}
		
	}
	
	
	@RequestMapping(value = "pendingUser", method = RequestMethod.GET)
	public String pendingUser(HttpServletRequest request){
		logger.info("查看待审核审核用户信息-------");
		try {
			List<UserLoginBO> userInfo = UseRService.findPendingUser(request);
			request.getSession().setAttribute("PendingUserList", userInfo);
		}catch (ServiceException serviceE){
			return "adminjsp/index";
		}catch (Exception e) {
			logger.error("select news failed! ", e);
			return "adminjsp/index";
		}

		return "adminjsp/pendingUser";
	}
	
	//审核用户
	@RequestMapping(value = "auditUser", method = RequestMethod.GET)
	public void auditUser(HttpServletRequest request,HttpServletResponse response) throws IOException{
		logger.info("审核审核用户信息-------");
		String userId =  request.getParameter("userId");
		response.setContentType("text/html;charset=utf8");
		PrintWriter out = response.getWriter();
		int id = 0;
		if (StringUtil.isNotBlank(userId)) {
			id = Integer.parseInt(userId);
		}
		try{
			UseRService.auditUserInfo(id);
			response.sendRedirect("pendingUser");
		}catch (ServiceException serviceE){
			out
			.print("<script type='text/javascript'>alert('审核失败');history.go(-1);</script>");
		}catch (Exception e) {
			logger.error("select news failed! ", e);
			out
			.print("<script type='text/javascript'>alert('审核失败');history.go(-1);</script>");
		}
	}
	
	@RequestMapping(value = "auditedUser", method = RequestMethod.GET)
	public String auditedUser(HttpServletRequest request){
		logger.info("查看已审核用户信息-------");
		try {
			List<UserLoginBO> userInfo = UseRService.findAllUser(request);
			request.getSession().setAttribute("userInfoList", userInfo);
		}catch (ServiceException serviceE){
			return "adminjsp/index";
		}catch (Exception e) {
			logger.error("select news failed! ", e);
			return "adminjsp/index";
		}

		return "adminjsp/auditedUser";
	}
	
	@RequestMapping(value = "deleteUser", method = RequestMethod.GET)
	public void deleteUser(HttpServletRequest request,HttpServletResponse response) throws IOException{
		request.setCharacterEncoding("utf8");
		response.setContentType("text/html;charset=utf8");
		String userId =  request.getParameter("userId");
		PrintWriter out = response.getWriter();
		int id = 0;
		if (StringUtil.isNotBlank(userId)) {
			id = Integer.parseInt(userId);
		}
		logger.info("根据ID查找管理员信息-------");
		try {
			UseRService.deleteUserById(id);
			response.sendRedirect("pendingUser");
		}catch (ServiceException serviceE){
			out
			.print("<script type='text/javascript'>alert('用户删除失败');history.go(-1);</script>");
		}catch (Exception e) {
			logger.error("select admin failed! ", e);
			out
			.print("<script type='text/javascript'>alert('用户删除失败');history.go(-1);</script>");
		}
		
	}
	
	@RequestMapping(value = "deleteAuditedUser", method = RequestMethod.GET)
	public void deleteAuditedUser(HttpServletRequest request,HttpServletResponse response) throws IOException{
		request.setCharacterEncoding("utf8");
		response.setContentType("text/html;charset=utf8");
		String userId =  request.getParameter("userId");
		PrintWriter out = response.getWriter();
		int id = 0;
		if (StringUtil.isNotBlank(userId)) {
			id = Integer.parseInt(userId);
		}
		logger.info("根据ID查找管理员信息-------");
		try {
			UseRService.deleteUserById(id);
			response.sendRedirect("auditedUser");
		}catch (ServiceException serviceE){
			out
			.print("<script type='text/javascript'>alert('用户删除失败');history.go(-1);</script>");
		}catch (Exception e) {
			logger.error("select admin failed! ", e);
			out
			.print("<script type='text/javascript'>alert('用户删除失败');history.go(-1);</script>");
		}
		
	}
	
	//按分类查询任务
	@RequestMapping(value = "selectTaskByCate", method = RequestMethod.GET)
	public void selectTaskByCate(HttpServletRequest request,HttpServletResponse response) throws IOException{
		String errorMessage = "查询失败";
		request.setCharacterEncoding("utf8");
		response.setContentType("text/html;charset=utf8");
		PrintWriter out = response.getWriter();
		String cateId =  request.getParameter("cateId");
		int id = 0;
		if (StringUtil.isNotBlank(cateId)) {
			id = Integer.parseInt(cateId);
		}
		logger.info("根据分类ID查找任务信息-------");
		try {
			List<TaskVO> cateInfo = taskService.selectTaskByCate(id);
			request.getSession().setAttribute("adminTaskInfList", cateInfo);
		}catch (ServiceException serviceE){
			errorMessage = serviceE.getMessage();
			out
			.print("<script type='text/javascript'>alert('分类查询失败');history.go(-1);</script>");
		}catch (Exception e) {
			out
			.print("<script type='text/javascript'>alert('分类查询失败');history.go(-1);</script>");
		}
		response.sendRedirect("taskList");
	}
		
	
	
	@RequestMapping(value = "logout", method = RequestMethod.GET)
	public String logout(HttpServletRequest request){
		logger.info("系统管理员退出登录");
		request.getSession().invalidate();
		return "adminjsp/login";
	}
}
